package io.renren.modules.sys.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.ProductService;
import io.renren.modules.sys.service.StockpurposeitemService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.service.StockpurposeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-03-26 15:35:07
 */
@RestController
@RequestMapping("sys/stockpurpose")
public class StockpurposeController {
    @Autowired
    private StockpurposeService stockpurposeService;

    @Autowired
    private ProductService productService;

    @Autowired
    private StockpurposeitemService stockpurposeitemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:stockpurpose:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = stockpurposeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:stockpurpose:info")
    public R info(@PathVariable("id") Integer id){
        StockpurposeEntity stockpurpose = stockpurposeService.getById(id);

        return R.ok().put("stockpurpose", stockpurpose);
    }

    /**
     * 信息
     */
    @RequestMapping("/infoById/{id}")
    @RequiresPermissions("sys:stockpurpose:infoById")
    public R infoById(@PathVariable("id") Integer id){
        StockpurposeVOEntity stockpurpose = stockpurposeService.getInfoById(id);

        return R.ok().put("stockpurpose", stockpurpose);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:stockpurpose:save")
    public R save(@RequestBody String str){
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        JSONObject json = JSONObject.fromObject(str);
        JSONArray arrayProduct = JSONArray.fromObject(json.get("products"));

        int stock = 0;//商品数量
        int ykcount = 0;//盈亏数量
        BigDecimal profitlossmoney = new BigDecimal(0);

        for(int i=0;i<arrayProduct.size();i++){
            ProductEntity product = productService.getById(arrayProduct.getJSONObject(i).getInt("id"));
            stock += arrayProduct.getJSONObject(i).getInt("stock");
            ykcount += arrayProduct.getJSONObject(i).getInt("ykcount");
            profitlossmoney = profitlossmoney.add(product.getCostprice().multiply(new BigDecimal(arrayProduct.getJSONObject(i).getInt("ykcount"))));
        }

        StockpurposeEntity stockpurpose = new StockpurposeEntity();
        stockpurpose.setPurposenumber("PD"+sdf.format(new Date()));
        stockpurpose.setCreatetime(new Date());
        stockpurpose.setCreateuser(Integer.parseInt(user.getUserId().toString()));
        stockpurpose.setShelvesid(json.getInt("shelvesid"));
        stockpurpose.setNum(stock+ykcount);
        stockpurpose.setProfitlossnum(ykcount);
        stockpurpose.setProfitlossmoney(profitlossmoney);

        stockpurposeService.save(stockpurpose);

        for(int i=0;i<arrayProduct.size();i++){
            StockpurposeitemEntity stockpurposeitemEntity = new StockpurposeitemEntity();
            stockpurposeitemEntity.setProductid(arrayProduct.getJSONObject(i).getInt("id"));
            stockpurposeitemEntity.setPurposeid(stockpurpose.getId());
            stockpurposeitemEntity.setCount(arrayProduct.getJSONObject(i).getInt("stock"));
            stockpurposeitemEntity.setPurposecount(arrayProduct.getJSONObject(i).getInt("count"));
            stockpurposeitemService.save(stockpurposeitemEntity);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:stockpurpose:update")
    public R update(@RequestBody StockpurposeEntity stockpurpose){
        ValidatorUtils.validateEntity(stockpurpose);
        stockpurposeService.updateById(stockpurpose);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:stockpurpose:delete")
    public R delete(@RequestBody Integer[] ids){
        stockpurposeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
